<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */
namespace App\Eloquent\Ygt;
use Illuminate\Database\Eloquent\SoftDeletes;
use Framework\Services\Push\JPush\JPushService;
use DB;

class Message extends DbEloquent{

    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $table = 'ygt_message';

    //当前月那天没消息
    public static function monthMessage($whereArr,$userId,$type=0)
    {
        $startTime          = isset($whereArr['start_time']) ? $whereArr['start_time'] : 0;
        $companyId          = $whereArr['company_id'];

        $nowTime            = $_SERVER['REQUEST_TIME'];
        $monthTime          = $startTime == 0 ? $nowTime : $startTime;
        $monthDays          = date('t',$monthTime);//多少天
        $yearNumber         = date('Y',$monthTime);//年
        $monthNumber        = date('n',$monthTime);//几月
        $monthFirst         = $yearNumber.'-'.$monthNumber.'-1';
        $monthTimeFirst     = strtotime($monthFirst);
        $monthTimeLast      = $monthTimeFirst + $monthDays*86400 +1;

        $join               =[
            [ 'table'=>'ygt_user_message as a', 'field_l'=>'a.message_id', 'field_c'=>'=', 'field_r'=>'ygt_message.id' ],
        ];
        $where              = [
            'ygt_message.company_id'=>$companyId,
            'ygt_message.created_at'=>['between',[$monthTimeFirst,$monthTimeLast]],
            'a.user_id'=>$userId, 'a.deleted_at'=>null
        ];
        $typeArr           = self::getAppSearchTypeArrByType( $type );
        if( $typeArr ){
            $where['ygt_message.type']      = ['in',$typeArr];
        }
        $orderby            = [ 'ygt_message.id', 'desc' ];
        $fileds             = 'ygt_message.id,ygt_message.type,ygt_message.type_id,ygt_message.title,';
        $fileds             .= 'ygt_message.content,ygt_message.created_at,';
        $fileds             .= 'a.type_status,a.is_read';
        $collection         = self::getList( $where, $fileds, '', '', $orderby, '',$join );
        $list               = $collection->toArray();
        $result             = [];
        foreach($list as $key=>$val)
        {
            $date           = date('Y-m-d',$val['created_at']);
            $result[]       = $date;
        }
        //$result             = array_unique($result);
        $ymdResult          = [];
        foreach($result as $key=>$val)
        {
            if(!in_array($val,$ymdResult))
            {
                $ymdResult[]    = $val;
            }
        }
        return $ymdResult;
    }


}